<template>
  <div id="usertext">
    <textarea v-model="content" placeholder="按 Ctrl + Enter 发送" @keyup="addMessage" />
  </div>
</template>

<script>
export default {
  name: 'Usertext',
  data() {
    return {
      content: ''
    }
  },
  computed: {
    currentSession() {
      return this.$store.state.chat.currentSession
    }
  },
  methods: {
    addMessage(e) {
      if (e.ctrlKey && e.keyCode === 13 && this.content.length) {
        // eslint-disable-next-line no-new-object
        var msg = new Object()
        msg.to = this.currentSession.username
        msg.content = this.content
        this.$store.state.chat.stomp.send('/ws/chat', {}, JSON.stringify(msg))
        this.$store.commit('chat/addMessage', msg)
        this.content = ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
#usertext {
	position: absolute;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 30%;
  border-top: solid 1px #DDD;

  > textarea {
    padding: 10px;
    width: 100%;
    height: 100%;
    border: none;
    outline: none;
  }
}
</style>
